package views;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.R;

import java.util.ArrayList;
import java.util.List;

import app.MyApplication;
import automatchingview.AutoMatchingPopuWindow;
import dialog.CenterDialog;
import greendao.UserDao;
import greendao.entity.User;

/**
 * Created by Jianqi on 2017/9/27.
 * 新建指定监护人View
 */

public class ZdjhrView extends RelativeLayout implements View.OnClickListener {

    public TextView auto_fzr;
    public TextView et_dh;
    public ImageView iv_add, iv_delect;
    //在同级ZdfzrView中是否为第一个
    private boolean isFirastView;
    private CenterDialog mCenterDialog;
    private UserDao mUserDao;
    PopupWindow mPopu;
    List<User> mSourceList;
    View view;

    public ZdjhrView(Context context) {
        super(context);
        initView();
        initDate();
        initListen();
    }

    public ZdjhrView(Context context, boolean isFirastView) {
        super(context);
        this.isFirastView = isFirastView;
        initView();
        initDate();
        initListen();
    }

    public ZdjhrView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initDate();
        initListen();
    }

    public ZdjhrView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initDate();
        initListen();
    }

    public void setJhrxm(String s) {
        auto_fzr.setText(s);
    }

    public void setJhrdh(String s) {
        et_dh.setText(s);
    }

    private void initView() {
        view= inflate(getContext(), R.layout.zdjhrview, this);
        auto_fzr = (TextView) findViewById(R.id.auto_fzr);
        et_dh = (TextView) findViewById(R.id.et_dh);
        iv_add = (ImageView) findViewById(R.id.iv_add);
        iv_delect = (ImageView) findViewById(R.id.iv_delect);
        ViewHolder holder = new ViewHolder();
        holder.auto_fzr = auto_fzr;
        holder.et_dh = et_dh;
        holder.iv_add = iv_add;
        holder.iv_delect = iv_delect;
        this.setTag(holder);
    }

    private void initDate() {
        if (isFirastView) {
            iv_add.setVisibility(VISIBLE);
            iv_delect.setVisibility(GONE);
            iv_add.setOnClickListener(this);
        } else {
            iv_add.setVisibility(GONE);
            iv_delect.setVisibility(VISIBLE);
            iv_delect.setOnClickListener(this);
        }
        mUserDao = MyApplication.getDaoSession().getUserDao();
        mSourceList=mUserDao.loadAll();
    }

    private void initListen() {

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final AutoMatchingPopuWindow window=new AutoMatchingPopuWindow();
                window.Builder(getContext())
                        .setOutsideTouchable(false)
                        .setSourceList(mSourceList)
                        .setEditTextWatcher(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                            }

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {

                            }

                            @Override
                            public void afterTextChanged(Editable s) {
                                    if(s.length()==0){
                                        window.UpListViewAdapter(mSourceList);
                                    } else {
                                        window.UpListViewAdapter(mUserDao.queryBuilder()
                                                .where(UserDao.Properties.LjyyxWorkerName.like("%"+s.toString()+"%"))
                                                .list());
                                    }
                            }
                        })
                        .setAutoMatchingPopuClickListener(new AutoMatchingPopuClickListener<User>() {
                            @Override
                            public void OnAutoMatchingPopClicked(List<User> list) {
                                if(list.size()==0){
                                    Toast.makeText(getContext(),"请选择一个监护人",Toast.LENGTH_SHORT).show();
                                }
                                auto_fzr.setText(list.get(0).getLjyyxLoginName());
                                et_dh.setText(list.get(0).getLjyyxWorkerTel());
                            }
                        });
                mPopu=window.CreatePopu();
                mPopu.showAtLocation(view, Gravity.RIGHT,0,0);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_add:
                ((ViewGroup) getParent()).addView(new ZdjhrView(getContext(), false));
                break;
            case R.id.iv_delect:
                //若该View中存在数据，显示删除提示框
                if (!auto_fzr.getText().toString().equals("") || !et_dh.getText().toString().equals("")) {
                    mCenterDialog = new CenterDialog(getContext(), new CenterDialog.CenterClick() {
                        @Override
                        public void confirmListener() {
                            ((ViewGroup) getParent()).removeView(ZdjhrView.this);
                            mCenterDialog.dismiss();
                        }

                        @Override
                        public void cancelListener() {
                            mCenterDialog.dismiss();
                        }
                    });
                    mCenterDialog.setMessage("该记录存在监护人信息，确定删除？");
                    mCenterDialog.show();
                } else {
                    ((ViewGroup) getParent()).removeView(ZdjhrView.this);
                }
                break;

        }
    }

    public class ViewHolder {
        TextView auto_fzr;
        TextView et_dh;
         ImageView iv_add, iv_delect;
    }

    public void cancelOnclick(){
        this.setOnClickListener(null);
    }

}
